<template>
  <div class="questions-randoms-container">
    <div class="app-container">
      <!-- 主题列表头部模块 -->
      <div class="questions-randoms-top">
        <div class="randoms-top-left">
          <label for="" style="padding-left: 10px">关键字</label>
          <el-input
            placeholder="根据编号搜索"
            class="rand-input"
            v-model="radData.keyword"
          ></el-input>
          <span style="float: right">
            <el-button class="top-btn" @click="ipt = ''">清除</el-button>
            <el-button type="primary" @click="onDemandSearch">搜索</el-button>
          </span>
        </div>
      </div>
      <div class="el-card">
        <el-alert :closable="false" class="el-icon-info"
          >数据一共{{ counts }}条</el-alert
        >
      </div>
      <!-- 表格模块 -->
      <el-table :data="tableData" stripe class="sub-tab">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column prop="questionType" label="题型" width="80%">
        </el-table-column>
        <el-table-column label="题目编号">
          <template slot-scope="{ row }">
            <p
              v-for="(item, index) in row.questionIDs"
              :key="index"
              @click="showProview(row)"
              class="proview"
            >
              {{ item.number }}
            </p>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间" width="160%">
        </el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间(s)"
        ></el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
        ></el-table-column>
        <el-table-column prop="userName" label="录入人"></el-table-column>
        <!-- 按钮 -->
        <el-table-column label="操作" width="80%">
          <template slot-scope="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delQuestion(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 底部分页模块 -->
      <div class="pagination">
        <el-pagination
          background
          :total="counts"
          :page-sizes="[10, 20, 30, 40]"
          layout="prev,pager,next,sizes,jumper"
          @current-change="changePage"
          @size-change="sizeChagePage"
          :page-size="20"
        >
        </el-pagination>
      </div>
    </div>
    <Proview
      :ProviewdialogVisible="ProviewdialogVisible"
      @proviewClose="proviewClose"
      :ProviewData="ProviewData"
    ></Proview>
  </div>
</template>

<script>
// 引入API接口
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
// 引入判断
import { questionType } from '@/api/hmmm/constants'
// 引入组件
import Proview from '@/components/Proview'
export default {
  components: {
    Proview
  },
  data () {
    return {
      tableData: [],
      radData: {
        // 当前页面
        page: 1,
        // 当页请求的数量
        pagesize: 20,
        // 搜索关键字
        keyword: ''
      },
      // 总数
      counts: null,
      // 组件弹窗是否显示
      ProviewdialogVisible: false,
      // 传递的数据对象
      ProviewData: {}
    }
  },
  created () {
    this.getRandoms()
  },
  methods: {
    // 发送请求，获取页面数据
    async getRandoms () {
      const res = await randoms(this.radData)
      console.log(res)
      // 对获取的数据中题型进行判断更换
      // 接口返回的是字符串，我们要将字符串转化为数字
      res.data.items.forEach((item) => {
        if (Number(item.questionType) === questionType[0].value) {
          // 如果questionType是1的话，这表示是单选
          item.questionType = questionType[0].label
        } else if (Number(item.questionType) === questionType[1].value) {
          // 如果questionType是2的话，这表示是多选
          item.questionType = questionType[1].label
        } else if (Number(item.questionType) === questionType[2].value) {
          // 如果questionType是3的话，这表示是简答
          item.questionType = questionType[2].label
        }
      })
      this.tableData = res.data.items
      // 获取总数
      this.counts = res.data.counts
      // console.log(this.tableData)
    },
    // 按需搜索
    onDemandSearch () {
      this.getRandoms()
    },
    // 页码发生改变的时候，发送的请求
    changePage (data) {
      this.radData.page = data
      this.getRandoms()
    },
    // 当选择条数下拉框中的条数发生改变
    sizeChagePage (data) {
      this.radData.pagesize = data
      this.getRandoms()
    },
    // 删除按钮
    delQuestion (data) {
      // 获取请求参数
      const id = {
        id: data.id
      }
      this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送请求
          await removeRandoms(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          // 更新渲染页面
          this.getRandoms()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 点击题型编号，跳出弹窗
    async showProview (data) {
      console.log(data)
      // console.log(data.questionIDs[0].id)
      // 显示弹窗
      this.ProviewdialogVisible = true
      // 发送请求，获取数据
      const res = await detail({ id: data.questionIDs[0].id })
      console.log(res)
      this.ProviewData = res.data
    },
    // 点击关闭
    proviewClose () {
      this.ProviewdialogVisible = false
      this.ProviewData = {}
    }
  }
}
</script>

<style scoped lang='less'>
.el-icon-info:before {
  font-size: 16px;
  margin-top: 5px;
}
.questions-randoms-container {
  margin: 10px 7px;
  padding: 5px;
  background-color: #fff;
}
.questions-randoms-top {
  padding-left: 5px;
  margin-bottom: 20px;
}
.rand-input {
  width: 188px;
  height: 32px;
  margin-left: 10px;
}
.el-card {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
.proview {
  margin: 0;
  color: #09f;
  cursor: pointer;
}
</style>
